﻿using System;
using System.Web;
using System.IO;

using TempEmployees.DAL;
using TempEmployees.BLL.EntitiesExtensions;
using TempEmployees.BLL.Database;

namespace TempEmployees.Web
{
    public partial class Main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionState state = (SessionState)Session[SessionState.SESSION_STATE];
            if (state == null)
            {
                ButtonLogout.Visible = false;
                string pageFileName = Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath);
                if (pageFileName != "Login.aspx") Response.Redirect(GUIServices.PAGE_LOGIN);
            }
            else if (!IsPostBack)
            {
                SetDateTimeDisplay();
                SetMainMenu((int)state.UserRole);
                ButtonLogout.Visible = true;
                User user = EUser.GetUserById(state.UserId);
                LabelHello.Text = string.Format("{2} {0} {1}", user.FirstName.Trim(), user.LastName.Trim(), "שלום");
                LabelRole.Text = string.Format("{0}{1}{2}", "(", state.UserRole.HebrewName().Trim(), ")");
            }

            if (!IsPostBack)
            {
                LabelTitle.Text = Page.Title;
            }
        }

        private void SetDateTimeDisplay()
        {
        }

        private void SetMainMenu(int roleId)
        {
            MainMenu.LoadMainMenu(roleId);
        }

        protected void ButtonLogout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect(GUIServices.PAGE_LOGIN);
        }
    }
}
